/**
 * 
 */
package com.shengruan.user.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.user.vo.SMSRecord;

/**
 * @author liubin
 *
 */
public class SMSRecordJdbc extends BaseJdbc{
	
	/***
	 * 保存
	 */
	public void save(SMSRecord smsRecord)
	{
		Connection conn = this.getConnection();
		String sql = "insert into SMSRecord (" +
		"telephone,ip,time,code,state)" +
		" values(?,?,?,?,?)";
		try {
			this.execute(conn, sql,smsRecord.getTelephone(),smsRecord.getIp(),smsRecord.getTime(),smsRecord.getCode(),smsRecord.getState());
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}
	
	/***
	 * 更新State
	 */
	public void updateState(int id)
	{
		Connection conn = this.getConnection();
		String sql = "update SMSRecord set state=1 where id=?";
		try {
			this.execute(conn, sql,id);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}	
	}
	/**
	 * 根据手机号码获得
	 * @param telephone
	 * @return
	 */
	public SMSRecord getByTelephone(String telephone)
	{
		Connection conn = this.getConnection();
		String sql = "select * from SMSRecord where telephone=? order by time desc limit 0,1";
		SMSRecord smsRecord = null;
		try
		{
			smsRecord = this.query(conn, sql, new BeanHandler<SMSRecord>(SMSRecord.class),new Object[]{telephone});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
		return smsRecord;
	}
	
	

}
